package com.controller;

import com.model.OrderModel;
import com.model.PageBeanMode;
import com.services.IOrderServices;
import com.services.impl.OrderServicesImpl;
import net.sf.json.JSONObject;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @Program: exam
 * @ClassName: OrderPageServletController
 * @Description: 用户查看自己的订单
 * @Author: li
 * @Create: 2024-12-23 17:03
 */
@WebServlet("/OrderPageServlet")
public class OrderPageServletController extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) 
            throws ServletException, IOException {
        response.setContentType("application/json;charset=UTF-8");
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");

        Map<String, Object> result = new HashMap<>();
        
        try {
            // 获取分页参数
            String currentPage = request.getParameter("currentPage");
            String pageSize = request.getParameter("pageSize");
            
            // 设置默认值
            if (currentPage == null || currentPage.isEmpty()) {
                currentPage = "1";
            }
            if (pageSize == null || pageSize.isEmpty()) {
                pageSize = "10";
            }

            // 获取查询参数
            Map<String, String[]> parameterMap = request.getParameterMap();
            
            // 调用服务获取分页数据
            IOrderServices orderServices = new OrderServicesImpl();
            PageBeanMode<OrderModel> pageBeanMode = orderServices.pageOrder(currentPage, pageSize, parameterMap);
            
            // 返回结果
            result.put("code", 0);
            result.put("msg", "success");
            result.put("data", pageBeanMode.getList());
            result.put("total", pageBeanMode.getTotalData());
            
        } catch (Exception e) {
            e.printStackTrace();
            result.put("code", 500);
            result.put("msg", "获取订单列表失败：" + e.getMessage());
        }
        
        response.getWriter().write(JSONObject.fromObject(result).toString());
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) 
            throws ServletException, IOException {
        doGet(request, response);
    }
}
